import { reactive, provide } from "vue";
import { Api } from "../../api/api.js";
import { ElNotification } from 'element-plus';
import router from "../../router/index.js";
import { GJ_nowTime } from "../../../static/js/tool.js";
import { aside } from "../../../static/sideData/side.js";

function homeJS() {
    let status = reactive({
        //未读邮件数量
        emailLen:0,
        //点击头像显示隐藏信息栏
        centerDialogVisible: false,
        //显示当前时间
        time: GJ_nowTime("date-time", "-"),
        //隐藏信息栏 input的display值
        core: {
            userN: true,
            userT: true,
            userM: true,
            userZ: true,
            userC: true,
        },
        findData: {
            user:"",
            limit: 12,
            page: 1,
            pages: "",
            total: "",
            createName:null
        },
        // 自己的数据信息
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),
        //导航栏信息
        aside: aside,
    });
    //更新当前时间
    setInterval(() => {
        status.time = GJ_nowTime("date-time", "-");
    }, 1000);

    let methodFun = {
        //退出登录
        goLogin: () => {
            Api.deleteToken({ user: status.userinfo.user }).then(() => {
                window.sessionStorage.removeItem("userinfo");
                router.push("/login");

            })
        },
        //修改用户信息
        changeCore: (val) => {
            console.log(val == "M", 2332);
            switch (val) {
                case "M":
                    status.core.userM = !status.core.userM;
                    break;
                case "Z":
                    status.core.userZ = !status.core.userZ;
                    break;
                case "C":
                    status.core.userC = !status.core.userC;
                    break;
                case "N":
                    status.core.userN = !status.core.userN;
                    break;
                case "T":
                    status.core.userT = !status.core.userT;
                    break;
                default:
                    if (status.userinfo.integral >= 10) {
                        status.userinfo.base = 1;
                        let changeIntegral = {
                            user: status.userinfo.user,
                            changeNumber: -10,
                            number: status.userinfo.integral,
                            type: "修改资料",
                            createTime: GJ_nowTime("date-time", "-")
                        };
                        //修改用户积分信息
                        Api.integralChange(changeIntegral).then((res) => {
                            console.log(res);
                        });
                        Api.datumChange(status.userinfo).then((res) => {
                            if (res.data != false) {
                                ElNotification({
                                    type: "success",
                                    message: "修改成功、积分-10",
                                });
                                window.sessionStorage.setItem(
                                    "userinfo",
                                    JSON.stringify(res.data)
                                );
                                status.userinfo.integral = JSON.parse(
                                    window.sessionStorage.getItem("userinfo")
                                ).integral;
                                status.core.userM = true;
                                status.core.userN = true;
                                status.core.userT = true;
                                status.core.userC = true;
                                status.core.userZ = true;
                            }
                        });

                    } else {
                        ElNotification({
                            type: "error",
                            message: "积分不足、无法修改",
                        });
                    }
            }
        },
        //导航栏权限控制
        isShowItem: (val) => {
            let control = status.userinfo.limitMange;
            if (val == "消费记录") {
                return control.isExpenseMange;
            } else if (val == "用户管理") {
                return control.isUserMange
            } else if (val == "数据管理") {
                return control.isDataMange
            } else if (val == "权限控制") {
                return control.isControlMange
            } else if (val == "相册集") {
                return control.isImageMange
            } else if (val == "媳妇、爱你") {
                return control.isLove
            } else if (val == "来聊天啊") {
                return control.isChatMange
            } else if (val == "邮件") {
                return control.isEmailMange
            }
            return true;
        },
        //需要重做
        integralAward: () => {
            if(status.userinfo.luckMeet){
                status.findData.user = [status.userinfo.user,status.userinfo.toUser];
            }else{
                status.findData.user = status.userinfo.user;
            }
            // 用户名
            // let user = status.userinfo.user;
            let isAwaed = status.userinfo.award;
            console.log(isAwaed,88888888);
methodFun.getSysList();
            if (!isAwaed) {
                Api.getData(status.findData).then((res) => {
                    console.log(res.data,456);
                    // let arr = res.data;
                    // let award = arr.filter((item) => {
                    //     return item.user == user;
                    // });
                    let integral = res.data.data.filter((item) => {
                        return item.changeName == "无";
                    });
                    let len = res.data.data.length;
                    let length = integral.length;
                    let baseIntegral = 2;
                    ElNotification({
                        type: "success",
                        title: `奖励${length * baseIntegral}积分`,
                        message: `截至目前为止：您发表说说数为${len},未修改数量为${length}.每天登录将增加${length * baseIntegral
                            }积分`,
                    });
                    status.userinfo.award = true;
                    window.sessionStorage.setItem("userinfo", JSON.stringify(status.userinfo));
                    let changeIntegral = {
                        user: status.userinfo.user,
                        changeNumber: length * baseIntegral,
                        number: status.userinfo.integral,
                        type: "登录奖励",
                        createTime: GJ_nowTime("date-time", "-")
                    };
                    Api.integralChange(changeIntegral);
                    methodFun.getInfo();
                });
            }
        },
        getInfo: () => {
            let user = JSON.parse(window.sessionStorage.getItem("userinfo")).user;
            Api.userFind({ user }).then((res) => {
                console.log(res.data.data, "9999999999");
                status.userinfo = res.data.data[0];
                window.sessionStorage.setItem("userinfo", JSON.stringify(status.userinfo));
            });
        },
        goluck: () => {
            router.push("/luck/luckMeet");
        },
        gomsg() {
            router.push("/system/message");
        },
        // 获取未读邮件数量
        getSysList() {
            let user = status.userinfo.user;
            Api.getSysList({ user }).then((res) => {

                status.emailLen = (res.data.filter((item)=>{
                    return item.isRead == false;
                })).length;
                console.log(status.emailLen,10102);
            });
        },
    }

    provide("getinfo", methodFun.getInfo);

    return {
        status,
        methodFun
    }
}
export default homeJS;